// LGUIListBox.cpp: implementation of the LGUIListBox class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LGUIListBox.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

LGUIListBox::LGUIListBox(int id, int _x, int _y, int _w, int _h) : LGUIContainer(_x,_y,_w,_h)
{
  ID=id;
  topItem=0;
  selectedItem=0;
  items=0;
  itemHeight=8;
  makeRoom(64);
  add(slider=new LGUIVSlider("",'SCRL',w-10,0,10,h-1,0,0,5));
  slider->setKnobHeight(0.6f);
  slider->hide(false);
/*
  addItem(TEXT("HELLO"));
  addItem(TEXT("HELLO1"));
  addItem(TEXT("HELLO2"));
  addItem(TEXT("HELLO3"));
  addItem(TEXT("HELLO4"));
  addItem(TEXT("HELLO5"));
  addItem(TEXT("HELLO6"));
  addItem(TEXT("HELLO7"));
  addItem(TEXT("HELLO8"));
  */
}

LGUIListBox::~LGUIListBox()
{
  clear();
  delete [] items;
}



void LGUIListBox::draw(int xoff, int yoff)
{
  if (isDirty)
  {
    buffer.rectFillFaded(x+xoff,y+yoff,x+xoff+w,y+yoff+h,COLOR(0),COLOR(0x302030));

    for (int i=0; i<getVisibleItems(); i++)
    {
      int num = i + topItem;
      if (num>=0 && num<numItems)
      {
        if (num==selectedItem)
        {
          buffer.rectFillDarkFaded(x+xoff,y+yoff+i*itemHeight+1,x+xoff+w-10,y+yoff+(i+1)*itemHeight+1,COLOR(0xffc000));
        }
        buffer.drawText(items[num].text,x+xoff+2,y+yoff+i*itemHeight+1,COLOR(0xFFFFFF),0,color_blit,x+xoff+w-10);
      }
    }
    LRect r=*this;
    r.w++;
    r.h++;
    buffer.addDirtyRect(r);
    isDirty=false;
  }
  LGUIContainer::draw(xoff, yoff);
}


void LGUIListBox::deliverMessage(LGUIMessage &m)
{
  if (m.data1=='SCRL')
  {
    scrollTo(m.data2);
  }
}

void LGUIListBox::penDown(int px, int py)
{
  LGUIContainer::penDown(px,py);
  if (isInside(px,py) && px<x+w-10)
  {
    py-=y+1;
    py/=itemHeight;
    py+=topItem;
    if (py >= topItem && py < topItem + getVisibleItems())
    {
      selectedItem=py;
      onClick(selectedItem);
      redraw();
    }
  }
}

void LGUIListBox::addItem(const TCHAR *txt)
{
  if (numItems<maxItems)
  {
    items[numItems++].text = txt;

    int scrollMax = numItems-getVisibleItems();
    if (scrollMax>0)
    {
      slider->show();
      slider->setLimits(0,scrollMax);
      slider->setKnobHeight(float(getVisibleItems())/float(numItems));
    }
    else
    {
      slider->hide();
    }
    
  }
}

void LGUIListBox::deleteItem(int i)
{
  if (i<0 || i>=numItems) return;
  if (numItems==1)
  {
    numItems=0;
  }
  else
  {
    for (int j=i; i<numItems-1; i++)
    {
      items[j] = items[j+1];
    }
    numItems--;
  }
}

void LGUIListBox::moveDown(int i)
{
  if (i<numItems-1)
  {
    if (i>=0)
    {
      LItem temp = items[i];
      items[i] = items[i+1];
      items[i+1]=temp;
    }
  }
}

void LGUIListBox::moveUp(int i)
{
  if (i<numItems)
  {
    if (i>=1)
    {
      LItem temp = items[i];
      items[i] = items[i-1];
      items[i-1]=temp;
    }
  }
}

int LGUIListBox::compare(const void *i1, const void *i2) //TODO: separate out directories later
{
  LItem *l1 = ( LItem *)i1;
  LItem *l2 = (LItem *)i2;

  return _tcscmp(l1->text,l2->text);
}

void LGUIListBox::sort()
{
  if (numItems>=2)
    qsort(items,numItems,sizeof(LItem),compare);
}
